﻿using SlideIdeaDocument.Format.UIElement.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace BoolanElement
{
    public abstract class BooTextShapesBaseElement : BooElement
    {
        public double TextMarginTop { get; set; }
        public double TextMarginLeft { get; set; }
        public double TextMaxHeight { get; set; }
        public double TextWidth { get; set; }

        private RichTextAlignment textAlignment;
        public RichTextAlignment TextAlignment 
        {
            get
            {
                return textAlignment;
            }
            set
            {
                textAlignment = value;
            }
        }

        private ShapeStyle shapeStyle;
        public ShapeStyle ShapeStyle
        {
            get
            {
                return shapeStyle;
            }
            set
            {
                shapeStyle = value;
            }
        }

        private Color shapeFillColor;
        public Color ShapeFillColor
        {
            get
            {
                return shapeFillColor;
            }
            set
            {
                shapeFillColor = value;
            }
        }

        public double ShapeWidth;
        public double ShapeHeight;

        private Color borderColor;
        public Color BorderColor 
        {
            get
            {
                return borderColor;
            }
            set
            {
                borderColor = value;
            }
        }

        private double borderWidth;
        public double BorderWidth 
        {
            get
            {
                return borderWidth;
            }
            set
            {
                borderWidth = value;
            }
        }

        public double ControlPanelTop { get; set; }
        public double ControlPanelLeft { get; set; }

        public string _Text;
        public string Text
        {
            set
            {
                _Text = value;

            }
            get
            {
                return _Text;
            }
        }
     

        /*public BooTextShapesBaseElement()
        {
            TextMarginTop = 0;
            TextMarginLeft = 0;
            TextMaxHeight = 200;
            TextWidth = 200;


            this.ShapeStyle = Configuration.ShapeStyle.NoBorderBKG;
            this.TextAlignment = Configuration.TextAlignment.CenterCenter;
            ShapeFillColor = Color.FromArgb(255, 85, 145, 206);
            BorderColor = Colors.Transparent;
            BorderWidth = 0;
            

            ControlPanelTop = 0;
            ControlPanelLeft = 0;
            Text = "";
        }*/

        public XElement SaveShapeStyle()
        {
            XElement styleElement = new XElement("Style");

            XElement textMarginTop = new XElement("TextMarginTop");
            textMarginTop.Value = TextMarginTop.ToString();
            XElement textMarginLeft = new XElement("TextMarginLeft");
            textMarginLeft.Value = TextMarginLeft.ToString();
            XElement textMaxHeight = new XElement("TextMaxHeight");
            textMaxHeight.Value = TextMaxHeight.ToString();
            XElement textWidth = new XElement("TextWidth");
            textWidth.Value = TextWidth.ToString();
            XElement fontAlignment = new XElement("TextAlignment");
            fontAlignment.Value = TextAlignment.ToString();
            XElement shapeStyle = new XElement("ShapeStyle");
            shapeStyle.Value = ShapeStyle.ToString();
            XElement shapeFillColor = new XElement("ShapeFillColor");
            shapeFillColor.Value = SerializeCast.ColorToString(ShapeFillColor);
            XElement shapeWidth = new XElement("ShapeWidth");
            shapeWidth.Value = ShapeWidth.ToString();
            XElement shapeHeight = new XElement("ShapeHeight");
            shapeHeight.Value = ShapeHeight.ToString();
            XElement borderColor = new XElement("BorderColor");
            borderColor.Value = SerializeCast.ColorToString(BorderColor);
            XElement borderWidth = new XElement("BorderWidth");
            borderWidth.Value = BorderWidth.ToString();
            XElement controlPanelTop = new XElement("ControlPanelTop");
            controlPanelTop.Value = ControlPanelTop.ToString();
            XElement controlPanelLeft = new XElement("ControlPanelLeft");
            controlPanelLeft.Value = ControlPanelLeft.ToString();

            styleElement.Add(textMarginTop);
            styleElement.Add(textMarginLeft);
            styleElement.Add(textMaxHeight);
            styleElement.Add(textWidth);
            styleElement.Add(shapeHeight);
            styleElement.Add(shapeWidth);
            styleElement.Add(fontAlignment);
            styleElement.Add(shapeStyle);
            styleElement.Add(borderColor);
            styleElement.Add(borderWidth);
            styleElement.Add(shapeFillColor);
            styleElement.Add(controlPanelTop);
            styleElement.Add(controlPanelLeft);

            return styleElement;
        }

        public XElement SaveData()
        {
            XElement element = new XElement("Data");
            XElement stringData = new XElement("StringData");
            element.Add(stringData);

            stringData.Value = RichTextCoder.RtfToBase64(this.Text);

            return element;
        }

        public void LoadData(XElement element)
        {
            XElement ele = element.Element("StringData");
            Text = RichTextCoder.Base64ToRtf(ele.Value);
        }


        public void LoadShapeStyle(XElement element)
        {
            TextMarginTop = Double.Parse(element.Element("TextMarginTop").Value);
            TextMarginLeft = Double.Parse(element.Element("TextMarginLeft").Value);
            TextMaxHeight = Double.Parse(element.Element("TextMaxHeight").Value);
            TextWidth = Double.Parse(element.Element("TextWidth").Value);
            TextAlignment = SerializeCast.StringToTextAlignment(element.Element("TextAlignment").Value);
            ShapeStyle = SerializeCast.StringToTextBoxStyle(element.Element("ShapeStyle").Value);
            BorderColor = SerializeCast.StringToColor(element.Element("BorderColor").Value);
            BorderWidth = Double.Parse(element.Element("BorderWidth").Value);
            ShapeFillColor = SerializeCast.StringToColor(element.Element("ShapeFillColor").Value);
            ShapeWidth = Double.Parse(element.Element("ShapeWidth").Value);
            ShapeHeight = Double.Parse(element.Element("ShapeHeight").Value);
            ControlPanelTop = Double.Parse(element.Element("ControlPanelTop").Value);
            ControlPanelLeft = Double.Parse(element.Element("ControlPanelLeft").Value);
        }


        public BooTextShapesBaseElement Clone(BooTextShapesBaseElement shapesElement)
        {
            BooTextShapesBaseElement ele = (BooTextShapesBaseElement)this.MemberwiseClone();
            return ele;
        }

        public virtual void Load(BooTextShapesBaseElement shapesElement)
        {
            this.Text = shapesElement.Text;
            this.TextAlignment = shapesElement.TextAlignment;
            this.TextMarginLeft = shapesElement.TextMarginLeft;
            this.TextMarginTop = shapesElement.TextMarginTop;
            this.TextMaxHeight = shapesElement.TextMaxHeight;
            this.TextWidth = shapesElement.TextWidth;
            this.UniqueID = shapesElement.UniqueID;
            this.ShapeStyle = shapesElement.ShapeStyle;
            this.ShapeHeight = shapesElement.ShapeHeight;
            this.ShapeFillColor = shapesElement.ShapeFillColor;
            this.ControlPanelTop = shapesElement.ControlPanelTop;
            this.ControlPanelLeft = shapesElement.ControlPanelLeft;
            this.BorderWidth = shapesElement.BorderWidth;
            this.BorderColor = shapesElement.BorderColor;
            this.ElementTransform = shapesElement.ElementTransform;
        }
    }
}
